package com.dgut.service.serviceImpl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.common.OnOrOff;
import com.dgut.common.SelectRole;
import com.dgut.mapper.ArticleMapper;
import com.dgut.pojo.Article;
import com.dgut.service.ArticleService;
import com.dgut.utils.PageRequestInfo;
import com.dgut.utils.PageResult;
import com.dgut.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Resource
    private ArticleMapper articleMapper;


    /**
     * start-zwb
     */
    //获取文章列表
    public Object getArticleList(PageRequestInfo pageRequestInfo){
        Page<Article> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<Article> resultPage = articleMapper.selectPage(page, null);
        PageResult result = PageUtils.getPageResult(resultPage);
        return result;
    }
    //获取已发布文章列表
    public Object getPublishedArticleList(PageRequestInfo pageRequestInfo){
        Page<Article> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("state",OnOrOff.ON);
        Page<Article> resultPage = articleMapper.selectPage(page, queryWrapper);
        PageResult result = PageUtils.getPageResult(resultPage);
        return result;
    }
    //根据文章标题获取文章
    public Object getArticleByName(String title) {
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        if(title!=null)
            queryWrapper.like("title",title);
        List<Article> articleList = articleMapper.selectList(queryWrapper);
        return articleList;
    }
    //根据文章id获取文章
    public Object getArticleById(Integer id)
    {
        return articleMapper.selectById(id);
    }
    //添加文章
    public Object addArticle(Article article)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        String s = OnOrOff.OFF;
        article.setState(s);
        if(articleMapper.insert(article)>0)
        {
            resultMap.put("message","更新成功");
            resultMap.put("result",true);
        }
        else {
            resultMap.put("message", "更新失败");
            resultMap.put("result", false);
        }
        return resultMap;
    }
    //修改文章
    public Object modifyArticle(Article article) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        if ( articleMapper.updateById(article) > 0) {
            resultMap.put("message", "更新成功");
            resultMap.put("result", true);
        } else {
            resultMap.put("message", "更新失败");
            resultMap.put("result", false);
        }
        return resultMap;
    }
    //删除文章
    public Object deleteArticle(Integer id) {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
                if ( articleMapper.deleteById(id) > 0) {
                    resultMap.put("message", "更新成功");
                    resultMap.put("result", true);
                } else {
                    resultMap.put("message", "更新失败");
                    resultMap.put("result", false);
                }
        return resultMap;
    }
    //上架文章
    public Object putOnArticle(Integer id)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        Article article = articleMapper.selectById(id);
        if(article!=null)
        {
            article.setState(OnOrOff.ON);
            queryWrapper.eq("id",article.getId());
            articleMapper.update(article,queryWrapper);
        }
        if(articleMapper.update(article,queryWrapper)>0){
            resultMap.put("message", "更新成功");
            resultMap.put("result", true);
        } else {
            resultMap.put("message", "更新失败");
            resultMap.put("result", false);
        }
        return resultMap;
    }
    //下架文章
    public Object takeDownArticle(Integer id)
    {
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QueryWrapper<Article> queryWrapper = new QueryWrapper<>();
        Article article = articleMapper.selectById(id);
        if(article!=null)
        {
            article.setState(OnOrOff.OFF);
            queryWrapper.eq("id",article.getId());
            articleMapper.update(article,queryWrapper);
        }
        if(articleMapper.update(article,queryWrapper)>0){
            resultMap.put("message", "更新成功");
            resultMap.put("result", true);
        } else {
            resultMap.put("message", "更新失败");
            resultMap.put("result", false);
        }
        return resultMap;
    }

    /**
     * end-zwb
     */


    /**
     * start-gxd
     */
    /**
     * end-gxd
     */


    /**
     * start-lzh
     */
    /**
     * end-lzh
     */

    /**
     * start-hwf
     */
    /**
     * end-hwf
     */
}
